package com.zhou.common.mybatis.template.shiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.common.lib.util.StringUtils;
import com.zhou.common.mybatis.template.shiro.constants.UserConstants;
import com.zhou.common.mybatis.template.shiro.entity.SysUser;
import com.zhou.common.mybatis.template.shiro.mapper.SysUserMapper;
import com.zhou.common.mybatis.template.shiro.service.ISysUserService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zxf
 * @since 2023-02-23
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUser selectUserByLoginName(String userName) {
        LambdaQueryWrapper<SysUser> eq = Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, userName);
        return getOne(eq);
    }

    @Override
    public Boolean updateUserInfo(SysUser sysUser) {
        return updateById(sysUser);
    }

    @Override
    public boolean registerUser(SysUser user) {
        user.setUserType(UserConstants.REGISTER_USER_TYPE);
        return save(user);
    }

    @Override
    public String checkLoginNameUnique(SysUser user) {
        Long userId = StringUtils.isNull(user.getId()) ? -1L : user.getId();
        LambdaQueryWrapper<SysUser> eq = Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, user.getLoginName());
        SysUser info = getOne(eq);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue()) {
            return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;
    }
}
